<template>
  <fs-page>
    <template #header>
      <div class="title">
        操作日志
        <fs-icon icon="icon-park:log" :spin="true" />
      </div>
    </template>
    <fs-crud ref="crudRef" v-bind="crudBinding">
      <template #actionbar-right>
        <n-dropdown type="primary" class="ml-1" :options="options" @select="handleMenuClick">
          <n-button type="error">
            批量删除
            <DownOutlined />
          </n-button>
        </n-dropdown>
      </template>
    </fs-crud>
  </fs-page>
</template>

<script lang="ts">
  import { defineComponent, ref, onMounted } from 'vue';
  import createCrudOptions from './crud';
  import { useExpose, useCrud } from '@fast-crud/fast-crud';
  import { useMessage, useDialog } from 'naive-ui';
  import { request } from '@/utils/http/service';
  export default defineComponent({
    name: 'OptLogForm',
    setup: function () {
      const message = useMessage();
      const dialog = useDialog();
      // crud组件的ref
      const crudRef = ref();
      // crud 配置的ref
      const crudBinding = ref();
      // 暴露的方法
      const { expose } = useExpose({ crudRef, crudBinding });
      // 你的crud配置
      const { crudOptions } = createCrudOptions({ expose });
      // 初始化crud配置
      // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
      const { resetCrudOptions } = useCrud({ expose, crudOptions });
      // 你可以调用此方法，重新初始化crud配置
      // resetCrudOptions(options)
      // 页面打开后获取列表数据
      onMounted(() => {
        expose.doRefresh();
      });

      const options = [
        {
          label: '全部',
          key: 0,
        },
        {
          label: '一天前',
          key: 1,
        },
        {
          label: '七天前',
          key: 7,
        },
        {
          label: '半个月前',
          key: 15,
        },
        {
          label: '一个月前',
          key: 30,
        },
      ];

      const handleMenuClick = (key: string | number) => {
        dialog.warning({
          title: '提示',
          content: `是否批量删除?`,
          positiveText: '确定',
          negativeText: '取消',
          onPositiveClick: () => {
            request({ url: `/authority/opt_logs/${key}`, method: 'delete' }).then(() => {
              message.success('批量删除成功', { duration: 3 });
              expose.doRefresh();
            });
          },
        });
      };
      return {
        handleMenuClick,
        crudBinding,
        crudRef,
        options,
      };
    },
  });
</script>
